{extend name="adminheader:header"/}
{block name="content"}
<script type="text/html" id="photos-item-tpl">
    <li id="saved-image{id}">
        <input id="photo-{id}" type="hidden" name="photo_urls[]" value="{filepath}">
        <input class="form-control" id="photo-{id}-name" type="text" name="photo_names[]" value="{name}"
               style="width: 200px;" title="图片名称">
        <img id="photo-{id}-preview" src="{imgs}" style="height:36px;width: 36px;cursor: pointer;"
             onclick="imagePreviewDialog(this.src);">
        <a href="javascript:uploadOneImage('图片上传','#photo-{id}');">替换</a>
        <a href="javascript:(function(){$('#saved-image{id}').remove();})();">移除</a>
    </li>
</script>
    <body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>修改商品</h5>
                        <a class="btn btn-default" href="{:url('/admin/Goods/index')}" style="float:right;">返回商品列表</a>
                    </div>
                    <div class="countdown"></div>
                    <div class="ibox-content">
                        <form action="{:url('/admin/Goods/editPost')}" method="post" class="form-horizontal js-ajax-form m-t">
                            <input type="hidden" name="id" value="{$list.id}" id="goodsid" />
                            <div class="form-group">
                                <label class="col-sm-3 control-label">商品分类<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                     <select class="form-control" name="cid">
                                        <option value="">- - - 请选择类型 - - -</option>
                                        {if condition="$category neq null"}
                                            {volist name="category" id="vo"}
                                                <option value="{$vo.id}" {$vo['pid']==0?'disabled':''} {$list['cid']==$vo['id']?'selected':''}>{$vo.name}</option>
                                            {/volist}
                                        {else/}
                                            <option value="" disabled="disabled">暂时无商品分类</option>
                                        {/if}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">商品名称<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <input name="g_name" type="text" class="form-control" value="{$list.g_name}" placeholder="请输入商品名称" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">商品图<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <ul id="photos" class="pic-list list-unstyled form-inline">
                                        {notempty name="post.photos"}
                                            {foreach name="post.photos" item="vo"}
                                                {php}$img_url=getPreviewUrl($vo['url']);{/php}
                                                <li id="saved-image{$key}">
                                                    <input id="photo-{$key}" type="hidden" name="photo_urls[]"
                                                           value="{$vo.url}">
                                                    <input class="form-control" id="photo-{$key}-name" type="text"
                                                           name="photo_names[]"
                                                           value="{$vo.name|default=''}" style="width: 200px;" title="图片名称">
                                                    <img id="photo-{$key}-preview"
                                                         src="{:getPreviewUrl($vo['url'])}"
                                                         style="height:36px;width: 36px;cursor: pointer;"
                                                         onclick="imagePreviewDialog(this.src);">
                                                    <a href="javascript:uploadOneImage('图片上传','#photo-{$key}');">替换</a>
                                                    <a href="javascript:(function(){$('#saved-image{$key}').remove();})();">移除</a>
                                                </li>
                                            {/foreach}
                                        {/notempty}
                                    </ul>
                                    <a href="javascript:uploadMultiImage('图片上传','#photos','photos-item-tpl');"
                                       class="btn btn-sm btn-default">选择图片</a>
                                </div>
                            </div>
                            <div class="form-group" id="f1">
                                <label class="col-sm-3 control-label">商品规格<span class="form-required">*</span></label>
                                <img src="/admin/img/add.png" width="30" height="30" style="cursor: pointer;" id="addiname" />
                                <img src="/admin/img/timg.jpg" width="30" height="30" style="cursor: pointer;" id="removeiname" />
                            </div>
                            <div class="form-group" id="f2">
                                <label class="col-sm-3 control-label">商品规格单价<span class="form-required">*</span></label>
                                
                            </div>
                            <div class="form-group" id="f3">
                                <label class="col-sm-3 control-label">数量<span class="form-required">*</span></label>
                                
                            </div>
                            <div class="form-group" id="f4">
                                <label class="col-sm-3 control-label">商品佣金<span class="form-required">*</span></label>
                                
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">数量的单位<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <input name="g_unit" type="text" class="form-control" value="{$list.g_unit}" placeholder="数量的单位" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">当前价<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <input name="g_cprice" type="text" class="form-control" value="{$list.g_cprice}" placeholder="请输入当前价" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">原价<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <input name="g_oprice" type="text" class="form-control" value="{$list.g_oprice}" placeholder="请输入原价" />
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">展示地方<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                   <input type="radio" name="g_show" id="inlineRadio2" value="1" {$list['g_show']==1?'checked':''} style="cursor: pointer;margin-right:5px"/>普通商品&nbsp;&nbsp;&nbsp;&nbsp;
                                   <input type="radio" name="g_show" id="inlineRadio3" value="2" {$list['g_show']==2?'checked':''} style="cursor: pointer;margin-right:5px"/>官方活动
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">商品描述<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <textarea class="form-control" name="g_description" placeholder="请填写商品描述">{$list.g_description}</textarea>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">商品信息<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <script id="content" name="g_content" type="text/plain">{$list.g_content|htmlspecialchars_decode|stripslashes}</script>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">状态<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                   <input type="radio" name="g_status" id="inlineRadio1" value="1" {$list['g_status']==1?'checked':''} style="cursor: pointer;margin-right:5px"/>新添加&nbsp;&nbsp;&nbsp;&nbsp;
                                   <input type="radio" name="g_status" id="inlineRadio2" value="2" {$list['g_status']==2?'checked':''} style="cursor: pointer;margin-right:5px"/>上架&nbsp;&nbsp;&nbsp;&nbsp;
                                   <input type="radio" name="g_status" id="inlineRadio3" value="3" {$list['g_status']==3?'checked':''} style="cursor: pointer;margin-right:5px"/>下架
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">添加时间<span class="form-required">*</span></label>
                                <div class="col-md-6 col-sm-10">
                                    <input class="form-control js-bootstrap-datetime" type="text" name="addtime" value="{:date('Y-m-d H:i',$list['addtime'])}">
                                </div>
                            </div>
                            <input type="hidden" name="length" value="" id="length"/>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-3">
                                    <button class="btn btn-primary js-ajax-submit" type="submit" id="btn">保存</button>
                                    <a class="btn btn-default" href="{:url('/admin/Goods/index')}">返回</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
<script src="/admin/js/admin.js"></script>
 <!-- 配置文件 -->
<script type="text/javascript" src="/admin/js/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="/admin/js/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script type="text/javascript">
    var ue = UE.getEditor('content');
</script>
<script type="text/javascript">
    /* 获取商品的规格信息 */
    var id = $('#goodsid').val();
    $.ajax({
        type: "get",
        url: "/admin/Goods/getinfo",
        data: {id:id},
        contentType: "application/json",
        success: function(res) {
            var data = res.data;
            var iname = '';
            var iprice = '';
            var inumber = '';
            var icommission = '';
            for(var i = 0 ; i<data.length; i++) {

                iname += "<div class='col-md-1 col-sm-10'><input type='hidden' name='id"+i+"' value="+data[i].id+" class='hid' /><input name='i_name"+i+"' type='text' class='form-control inp_lis' value="+data[i].i_name+" placeholder='规格' /></div>";

                iprice += "<div class='col-md-1 col-sm-10'><input name='i_price"+i+"' type='text' class='form-control' value="+data[i].i_price+" placeholder='单价'' /></div>";

                inumber += "<div class='col-md-1 col-sm-10'><input name='i_number"+i+"' type='text' class='form-control' value="+data[i].i_number+" placeholder='数量'' /></div>";

                icommission += "<div class='col-md-1 col-sm-10'><input name='i_commission"+i+"' type='text' class='form-control' value="+data[i].i_commission+" placeholder='佣金'' /></div>";
            }

            $('#f1').children('label').after(iname);
            $('#f2').children('label').after(iprice);
            $('#f3').children('label').after(inumber);
            $('#f4').children('label').after(icommission);
        },
        error:function(data){
            layer.alert("服务器出错!");
        }
    });

    /* 商品的所有规格数据 */
    
    $('#addiname').click(function(){

        var iname = $("#f1").children('div');
        var iprice = $("#f2").children('div');
        var inumber = $("#f3").children('div');
        var icommission = $("#f4").children('div');
        {
            var lis=$(".inp_lis");
            var a = lis.length;
            for (var i=a;i<10;i++) {
                var k=$(this).index()
            }
            
        let s=k-1;
        
            if(iname.length<=10){
                $('#f1').children('div:last').after('<div class="col-md-1 col-sm-10"><input name="i_name'+s+'" type="text" class="form-control" value="" placeholder="规格" /></div>');
            }
            
            if(iprice.length<=10){
                $('#f2').children('div:last').after('<div class="col-md-1 col-sm-10"><input name="i_price'+s+'" type="text" class="form-control" value="" placeholder="单价" /></div>');
            }

            if(inumber.length<=10){
                $('#f3').children('div:last').after('<div class="col-md-1 col-sm-10"><input name="i_number'+s+'" type="text" class="form-control" value="" placeholder="数量" /></div>');
            }

            if(icommission.length<=10){
                $('#f4').children('div:last').after('<div class="col-md-1 col-sm-10"><input name="i_commission'+s+'" type="text" class="form-control" value="" placeholder="佣金" /></div>');
            }
        }

    });

    /*  移除商品规格 */
    $('#removeiname').click(function(){
        var iname = $("#f1").children('div');
        var iprice = $("#f2").children('div');
        var inumber = $("#f3").children('div');
        var icommission = $("#f4").children('div');

        if(iname.length>=2){

            var id = $('#f1').children('div:last').children('.hid').val();

           /* 删除商品规格 */
            $.get('/admin/goods/deleteInfo',{id:id},function(res){
                $('#f1').children('div:last').remove();
            });
        }

        if(iprice.length>=2){
           $('#f2').children('div:last').remove();
        }

        if(inumber.length>=2){
           $('#f3').children('div:last').remove();
        }

        if(icommission.length>=2){
           $('#f4').children('div:last').remove(); 
        }

    });

    /* 计算商品规格的长度 */
    $('#btn').click(function(){
        var iname = $("#f1").children('div');
        var len = iname.length;
        $('#length').val(len);
    });


</script>
{/block}